package app;

public class CurrencyConverterModel {
	final double INITIAL_NUMBER = -1;
	State state;
	double result;
	double value;
	
	Country selected_country;
	
public CurrencyConverterModel() {
		state = new State_Idle();
		result = INITIAL_NUMBER;
		value = INITIAL_NUMBER;
		this.selected_country = null;
	}

public void compute() throws Exception {
		if(this.state instanceof State_DoneBothInputs)
			result = value * selected_country.getFactor();
		state = state.compute();
		System.out.println(state);
		
	}

	public void countrySelected(Country c) {
		if(c == null) return;
		this.selected_country = c;
		state = state.countrySelected();
		System.out.println(state);
	}
	
	public void usDollarAmtModified(String txt) {
		try{
			value = Double.parseDouble(txt);
			if(value>=0)
				state=state.amountEntred();
			else
				state = state.textClear();
		}catch (Exception e) {
			state = state.textClear();
		}
		System.out.println(state);
	}

	public void clear() {
		state = state.clear();
		result = INITIAL_NUMBER;
		value = INITIAL_NUMBER;
		this.selected_country = null;
		System.out.println(state);
	}

	public double getResult() {
		return result;
	}
	
	public String getMsg() {
		return selected_country.toString();
	}

	public State getState() {
		return this.state;
	}	
}
